<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<link rel="stylesheet" href="/richText/bootstrap-combined.no-icons.min.css" type="text/css">
<link rel="stylesheet" href="/richText/index.css" type="text/css"></link>
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.0.2/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/ace.min.css">
<link rel="stylesheet" href="css/ace-skins.min.css">
<link rel="stylesheet" href="css/ace-responsive.min.css">
<style>
    body, html {
        width: 100%;
        height: 100%;
        background: #fff;
    }

    #editor {
        height: 200px;
    }

    #main-content {
        margin-left: 190px;
    }

    #page-content {
        margin: 0;
        background: #FFF;
        position: relative;
        top: 0%;
        left: -10%;
    }

    .modal-footer {
        background: #fff;
    }
</style>


<body>
<div id="main-content" class="clearfix">
    <div id="page-content" class="clearfix">


        <div class="page-header position-relative">
            <h1 style="color: #2679b5;">品牌商
                <small><i class="icon-double-angle-right"></i> 商品信息录入</small>
            </h1>
        </div>
        <!--<form class="form-search">-->
        <!--商品标题：-->
        <!--<input type="text" class="input-medium search-query">-->
        <!--<button onclick="return false;" class="btn btn-purple btn-small">Search <i class="icon-search icon-on-right"></i></button>-->
        <!--</form>-->
        <table id="table_bug_report" class="table table-striped table-bordered table-hover">
            <thead>
            <tr>
                <!--<th class="center">
                    <label><input type="checkbox" class="ace-checkbox-2"><span class="lbl"></span></label>
                </th>-->
                <th>商品标题</th>
                <th>商品价格</th>
                <th>商品库存</th>
                <th>sku编码</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody id="goodstable">

            <!--asddddddddddddddddddddddddddddddddddddddddddddddddddddddd-->

            </tbody>
        </table>

        <button class="btn btn-primary" onclick="gotoadd()" id="adnew">新增</button>

    </div>
</div>
<script src="js/jquery-1.9.1.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootbox.min.js"></script>
<script>
    var datalist = "";
    var mvo_id

    //如果没有添加公司信息，禁止新增按钮


    $(function () {
        var url = location.search;
        var Request = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1)　//去掉?号
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                Request[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        }

        mvo_id = Request["uid"]
        console.log(mvo_id);
        //将需要修改信息的商品ID传到该页面
        // $("#index").attr('href','/role?uid='+mvo_id);
    })


    // $(function () {
    //     $.ajax({
    //         type: 'get',
    //         url: '/selectSaveGoodsByPrimaryKey',
    //         data: {goodsID: 52 , mvoID:mvo_id},
    //         dataType: 'json',
    //         async: false,
    //         success:function (key) {
    //             alert(key.goodsNum)
    //         },
    //         error:function () {
    //             alert('ok')
    //         }
    //     })
    // })

    var updateflag = false
    $(function () {
        var $tbody = $("#goodstable");//表格
        $.ajax({
            type: 'post',
            url: '/goods/selectGoodsByCompanyID',
            data: {mvo_id: mvo_id},
            dataType: 'json',
            async: false,
            success: function (goods) {
                datalist = goods;
                var newHtml = '';
                for (var i = 0; i < goods.length; i++) {
                    var nu
                    $.ajax({
                        type: 'post',
                        url: '/savegoods/selectSaveGoodsByPrimaryKey',
                        data: {goodsID: goods[i].id, mvoID: mvo_id},
                        dataType: 'json',
                        async: false,
                        success: function (key) {
                            // alert(key.goodsNum)
                            nu = key.goodsNum
                        },
                        error: function () {
                            // alert('ok')
                        }
                    })
                    // var tmp = goods[i].title
                    // alert(tmp)
                    // for(i in good[i]){
                    //     console.log(i)
                    // }
                    newHtml += '<tr>';
                    /*newHtml += '          <td class="center">';
                    newHtml += '            <label><input type="checkbox" class="input"><span class="lbl"></span></label>';
                    newHtml += '          </td>';*/
                    newHtml += '          <td><a href="#" onclick="gotogoodsdetail(' + goods[i].id + ',' + nu + ',' + goods[i].brandId + ')">' + goods[i].title + '</a></td>';
                    newHtml += '          <td>￥' + goods[i].price + '</td>';
                    newHtml += '          <td class="hidden-480">' + nu + '</td>';
                    newHtml += '          <td class="hidden-phone">' + goods[i].sku + '</td>';
                    newHtml += '          <td>';
                    newHtml += '            <div class="inline position-relative" >';
                    newHtml += '              <button class="btn btn-mini btn-info" onclick="gotoedit(' + i + ')"><i class="icon-edit"></i></button>';
                    newHtml += '              <button class="btn btn-mini btn-danger" onclick="delgoods(' + goods[i].id + ')"><i class="icon-trash"></i></button>';
                    newHtml += '            </div>';
                    newHtml += '          </td>';
                    newHtml += '        </tr>';
                }
                $tbody.html(newHtml);
            },
            error: function () {
                alert("inseGoodsError")
            }
        })
    })


    function gotogoodsdetail(id, nu, brandid) {
        window.location.href = '/mvogoodsDetail?&brandid=' + brandid + '&num=' + nu+"&id="+id;
    }

    $(function () {
        document.getElementById('adnew').setAttribute("disabled", true)
        $.ajax({
            type: "post",
            url: "/user/selectCompanyByUid",
            dataType: "json",
            async: false,
            data: {id: mvo_id},
            success: function (company) {
                console.log(company)
                // alert('nice')
                document.getElementById('adnew').removeAttribute("disabled")
            },
            error: function () {
                // alert("请录入公司讯息")

            }
        })
    })

    function delgoods(id) {

        var x = confirm("确认删除商品?")
        if (x == true) {
            $.ajax({
                type: 'post',
                url: '/goods/deleteGoods',
                data: {id: id},
                dataType: 'text',
                success: function (re) {
                    alert(re)
                    window.location.reload()
                },
                error: function (re) {
                    alert("delGoodsError986" + re)
                    window.location.reload()
                }

            })

        }
    }


    $(function () {
        $('.ace-checkbox-2').change(function () {
            if ($('.ace-checkbox-2').prop('checked')) {
                $('.input').prop('checked', true);
            } else {
                $('.input').prop('checked', false);
            }
        })


        // $('.btn-danger').click(function(){
        //     bootbox.alert("删除成功！");
        // })

    })

    function gotoadd() {
        window.location.href = '/mvoaddGoods?uid=' + mvo_id;

    }

    function gotoedit(i) {
        var flag = 0
        // alert(datalist[i].image)
        window.location.href = '/mvoupdateGoods?id=' + datalist[i].id + '&uid=' + mvo_id + "&flag=" + flag + "&image=" + datalist[i].image;
    }

</script>
</body>
</html>
